package com.danskin.wechat.util;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.danskin.wechat.dto.MessageAnalyzeDto;

public class BeanUtil {
	
	

	public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {    
        if (map == null)  
            return null;  
  
        Object obj = beanClass.newInstance();  
  
        org.apache.commons.beanutils.BeanUtils.populate(obj, map);  
  
        return obj;  
    }    
      
    public static Map<?, ?> objectToMap(Object obj) {  
        if(obj == null)  
            return null;   
  
        return new org.apache.commons.beanutils.BeanMap(obj);  
    } 
    /***
     * 生成订单号
     * @return
     */
    public static String createOrderNumber(){
    	SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmssSSS");//设置日期格式
		String time = df.format(new Date());
		int num = getRandom();
		String order = time + String.valueOf(num);
		return order;
    } 
    /***
     * 随机生成3位数
     * @return
     */
    public static int getRandom() {
        int number = 0;
        while (true) {
            number = (int) (Math.random() * 1000);
            if (number >= 100 && number < 1000) {
                break;
            }
        }
        return number;
    }
    /**
     * 获取交易日期
     * @return
     */
    public static int tradeDate(){
    	SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");//设置日期格式
		return Integer.parseInt(df.format(new Date()));
    } 
    
    /**
     * list排序
     * @param list
     * @author makunsmac
     */
    public static void ListSort(List<MessageAnalyzeDto> list) {
    	         Collections.sort(list, new Comparator<MessageAnalyzeDto>() {
    	             @Override
    	             public int compare(MessageAnalyzeDto o1, MessageAnalyzeDto o2) {
    	                 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    	                 try {
    	                     Date dt1 = format.parse(o1.getRef_date());
    	                     Date dt2 = format.parse(o2.getRef_date());
    	                     if (dt1.getTime() > dt2.getTime()) {
    	                         return 1;
    	                     } else if (dt1.getTime() < dt2.getTime()) {
    	                         return -1;
    	                     } else {
    	                         return 0;
    	                     }
    	                 } catch (Exception e) {
    	                     e.printStackTrace();
    	                 }
    	                 return 0;
    	             }
    	         });
    	     }
    
 
  

    
    
    
}
